// miniprogram/pages/my/record.js
const db=wx.cloud.database()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationIndex: 0,
    navigationType: 'awarded-lottery',
    navigations: [{
        name: '中奖记录',
        type: 'awarded-lottery'
      },
      {
        name: '待开奖',
        type: 'waiting-lottery'
      },
      {
        name: '所有抽奖',
        type: 'total-lottery'
      }
    ],
    list:[]
  },

  onNavigationChange(e) {
    
    let navigationIndex = e.detail.navigationIndex;
    this.setData({
      navigationIndex: navigationIndex,
      navigationType: this.data.navigations[navigationIndex].type
    });
    if(navigationIndex==0){
      let oldlist=[]
      db.collection('game').where({
        _openid:this.data.openid
      }).get().then((res=>{
        console.log(res)
        for(let i=0;i<res.data.length;i++){
  db.collection('active_list').where({
  _id:  res.data[i].gameXY,
  HJ: _.elemMatch({
   _openid:this.data.openid
  })
  }
  ).get().then((res=>{
  console.log(res)
  if(res.data.length>0)  
  { oldlist.push(res.data[0])
      this.setData({
        list:oldlist
      })
    }
      else{
        this.setData({
          list:oldlist
        })
      }
  })
  )
        }
      }))
    }
    else if(navigationIndex==1){
     
      let oldlist=[]
      db.collection('game').where({
        _openid:this.data.openid
      }).get().then((res=>{
     
        for(let i=0;i<res.data.length;i++){
  db.collection('active_list').where({
  _id:  res.data[i].gameXY,
  HJXY:false
  }
  ).get().then((res=>{
  console.log(res)
  let newdata=res.data[0]
   oldlist.push(newdata)
    this.setData({
      list:oldlist
    })
    console.log(this.data.list)
  })
  )
        }
      }))
    }
    else if(navigationIndex==2){
      let oldlist=[]
      db.collection('game').where({
        _openid:this.data.openid
      }).get().then((res=>{
     
        for(let i=0;i<res.data.length;i++){
  db.collection('active_list').where({
  _id:  res.data[i].gameXY,
  
  }
  ).get().then((res=>{
  console.log(res)
  if(res.data.length>0)

  {    oldlist.push(res.data[0])
      this.setData({
        list:oldlist
      })}
  })
  )
        }
      }))
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
console.log(options)
    let externalNavigationIndex = Number(options.navigationIndex);
    if (!isNaN(externalNavigationIndex)) {
      this.setData({
        openid:options.openid,
        navigationIndex: externalNavigationIndex,
        navigationType: this.data.navigations[externalNavigationIndex].type
      });
  if(options.navigationIndex==0){
    let oldlist=[]
    db.collection('game').where({
      _openid:options.openid
    }).get().then((res=>{
      console.log(res)
      for(let i=0;i<res.data.length;i++){
db.collection('active_list').where({
_id:  res.data[i].gameXY,
HJ: _.elemMatch({
 _openid:options.openid
})
}
).get().then((res=>{
console.log(res)
if(res.data.length>0)

{    oldlist.push(res.data[0])
    this.setData({
      list:oldlist
    })}
})
)
      }
    }))
  }
  else if(options.navigationIndex==1){
    let oldlist=[]
    db.collection('game').where({
      _openid:options.openid
    }).get().then((res=>{
      console.log(res)
      for(let i=0;i<res.data.length;i++){
db.collection('active_list').where({
_id:  res.data[i].gameXY,
HJXY:false
}
).get().then((res=>{
console.log(res)
if(res.data.length>0)

{    oldlist.push(res.data[0])
    this.setData({
      list:oldlist
    })}
})
)
      }
    }))
  }
  else if(options.navigationIndex==2){
    let oldlist=[]
    db.collection('game').where({
      _openid:options.openid
    }).get().then((res=>{
      console.log(res)
      for(let i=0;i<res.data.length;i++){
db.collection('active_list').where({
_id:  res.data[i].gameXY,

}
).get().then((res=>{
console.log(res)
if(res.data.length>0)

{    oldlist.push(res.data[0])
    this.setData({
      list:oldlist
    })}
})
)
      }
    }))
  }
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  onQueryRecords() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

    this.selectComponent("#record-" + this.data.navigationType).onPullDownRefresh();

  },

  onReachBottom: function () {

    this.selectComponent("#record-" + this.data.navigationType).onReachBottom();

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})